//
//  NSString+Parameters.m
//  GoodFreshFood
//
//  Created by xinchuang1 on 15/4/15.
//  Copyright (c) 2015年 Edward. All rights reserved.
//

#import "NSString+Parameters.h"
#import <CoreFoundation/CoreFoundation.h>

@implementation NSString_Parameters

+ (NSString *) getResultURLWithPath:(NSString *)urlPath paramers:(NSDictionary *)parameters andBody:(NSDictionary *)body requestTYpe:(NSString *)requestTYpe{
    
    NSMutableDictionary *newDic = [NSMutableDictionary new];
    if (parameters != nil) {
        [newDic addEntriesFromDictionary:parameters];
    }
    
    NSString *parmString = [self HTTPBodyWithParameters:newDic requestTYpe:requestTYpe];
    NSString *resultStr =  [NSString stringWithFormat:@"%@%@?%@",kBaseURL,urlPath,parmString];
    if (parameters==nil) {
        resultStr = [NSString stringWithFormat:@"%@%@",kBaseURL,urlPath];
    }
    resultStr = [resultStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return resultStr;
}

+( NSString *)HTTPBodyWithParameters:( NSDictionary *)parameters requestTYpe:(NSString *)type{
    NSMutableArray *parametersArray = [[ NSMutableArray alloc ] init ];
    for ( NSString *key in [parameters allKeys ]) {
        id value = [parameters objectForKey :key];
        [parametersArray addObject :[ NSString stringWithFormat : @"%@=%@" ,key,value]];
    }
    NSString *tmpGetString=nil;
    if ([type isEqualToString:@"post"]) {
        tmpGetString=[parametersArray componentsJoinedByString:@"&"];
    }else{
        tmpGetString=[parametersArray componentsJoinedByString:@"&&"];
    }
    return  tmpGetString;//get ==&&    POST=&
}

@end
